import {checkBillId, COMMON, getCheckedRows} from "../../../main/util";
import CommonAction from "../../../actions/common";

export default class FormAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    // 初始化
    didMount = () => {
        // 统一订阅事件
        this.subscribe();
    };

    // 卸载
    willUnMount = () => {
        this.pubSub.unSubscribe('showViewContract');
    };

    // 事件订阅部分
    subscribe = () => {
        const {pubSub} = this;
        pubSub.subscribe('showViewContract', () => {
            this.showModel();
        });
    };

    // 获取查看合同数据
    getViewContractData = async () => {
        const {props} = this.comp;
        const {
            dispatch,
            transfer
        } = props;
        let billids;
        if (transfer.page === COMMON.pageMain) {
            billids = getCheckedRows(props, 'pk_hi_stapply');
        }else if (transfer.page === COMMON.pageDetail) {
            billids = transfer.billid;
        }
        if (!checkBillId(this.toast, billids, transfer)) return;
        let postData = {
            billids,
            pk_org: transfer.hrorgobj.refpk
        };

        let res = await dispatch({
            type: 'transfer/getViewContractData',
            payload: {
                postData: postData
            }
        });
        if (!res.success) return;
        // 查看合同表格信息
        let viewContractTableInfo = res.data.columInfo.map((item) => {
            return {
                ...item,
                title: item.name,
                dataIndex: item.code,
                key: item.code
            }
        });
        // 查看合同表格数据
        let viewContractTableData = res.data.columData;
        this.comp.setState({
            viewContractTableInfo,
            viewContractTableData,
        })
    }

    /**
     * 显示弹窗
     * @returns {Promise<void>}
     */
    showModel = async () => {
        await new Promise((resolve, reject) => {
            this.comp.setState({
                viewContractModalVisible: true
            }, resolve);
        });
        this.getViewContractData()
    };

    /**
     * 关闭弹窗
     */
    closeModal = () => {
        this.comp.setState({
            viewContractModalVisible: false
        });
    };
}